package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.PointsMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 小海
 * @since 2024-09-20
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class LearningPointsListener {

    private final IPointsRecordService pointsRecordService;

    /**
     * 监听签到消息
     *
     * @param message
     */

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            value = @Queue(value = "sign.points.queue", durable = "true"),
            key = MqConstants.Key.SIGN_IN
    ))
    public void listenSignMessage(PointsMessage message) {
        // 1.判断
        if (message == null) {
            log.error("message为null");
            return;
        }
        // 2.调用业务
        pointsRecordService.addPointsRecord(message, PointsRecordType.SIGN);
    }
}
